package com.wangbl.infomanager;

import org.json.JSONException;
import org.json.JSONObject;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tangye.android.http.Request.ResponseListener;
import com.wangbl.infomanager.http.Http;
import com.wangbl.infomanager.http.Response;
import com.wangbl.infomanager.utils.Logger;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class UserInfoShowActivity extends BaseActivity {
	

	private String url = "http://irmsma.ruc.edu.cn/rucjson/tz.php";
	private SharedPreferences sp;
	protected String TAG = "UserInfoShowActivity";
	private ImageView head_icon;
	private TextView name;
	private TextView sex;
	private TextView tv_date;
	private TextView email;
	private TextView grade;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_user_info_show);
		super.onCreate(savedInstanceState);
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		title_user.setVisibility(View.GONE);
		head_icon = (ImageView) findViewById(R.id.user_head_icon_iv);
		name = (TextView) findViewById(R.id.user_name);
		sex = (TextView) findViewById(R.id.user_sex);
		tv_date = (TextView) findViewById(R.id.user_in_date);
		email = (TextView) findViewById(R.id.user_email);
		grade = (TextView) findViewById(R.id.user_grade);
		doRequest();
	}
	
	private void doRequest(){
		//http://irmsma.ruc.edu.cn/rucjson/tz.php?username=2011997607-04 11:10:27.579: I/UserInfoShowActivity(16384): head:/images/zw.jpg
		//http://irmsma.ruc.edu.cn/images/zw.jpg
		String username = sp.getString("username", "");
		if(TextUtils.isEmpty(username)){
			toast("您已掉线,请重新登录...");
			return;
		}
		pd.show();
		Http http = new Http(url);
		http.setParam("username", username);
		
		http.asyncRequest(new ResponseListener<Response>() {

			@Override
			public void onErr(int arg0, String arg1, int arg2, Object... arg3) {
				toast(arg1);
				pd.dismiss();
				
			}

			@Override
			public void onResp(int arg0, Response arg1, Object... arg2) {
				Logger.i(TAG , "response:"+arg1.json);
				parser(arg1.json);
				pd.dismiss();
			}
		});
	}
	
	private void parser(String json){
		try {
			JSONObject obj = new JSONObject(json);
			String str_name = obj.getString("username");
			String str_date = obj.getString("date");
			String str_sex = obj.getString("sex");
			String str_eamil = obj.getString("email");
			String str_grade = obj.getString("grade");
			String icon_url = obj.getString("head");
			Logger.i(TAG, "head:"+icon_url);
			name.setText("姓        名：    "+str_name);
			sex.setText("性        别：    "+str_sex);
			tv_date.setText("入学时间：    "+str_date);
			email.setText("邮        箱：    "+str_eamil);
			grade.setText("班        级：    "+str_grade);
			if(!TextUtils.isEmpty(icon_url)){
				ImageLoader.getInstance().displayImage(Http.BASE_URL+icon_url, head_icon);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	@Override
	protected void interrupt() {

	}

}
